local skill = fk.CreateSkill {
  name = "hanqing__vh_alliance_skill",
}

Fk:loadTranslationTable{
  ["#hanqing__vh_alliance_skill"] = "选择一至两名角色，目标进入对应状态",
}

local U = require "packages/hanqing/util"

skill:addEffect("cardskill", {
  prompt = "#hanqing__vh_alliance_skill",
  target_tip = function(self, player, to_select, selected, selected_cards, card, selectable)
    if not selectable then return end
    if not to_select.chained or U.getAlliance(to_select) ~= "horizontal" then
      return "@@hanqing__vertical_alliance"
    elseif to_select.chained or U.getAlliance(to_select) == "vertical" then
      return "@@hanqing__horizontal_alliance"
    elseif U.getAlliance(to_select) == "horizontal" then
      return "un-chained"
    end
  end,
  min_target_num = 1,
  max_target_num = 2,
  mod_target_filter = Util.TrueFunc,
  target_filter = Util.CardTargetFilter,
  on_effect = function(self, room, effect)
    if not effect.to.chained or U.getAlliance(effect.to) ~= "horizontal" then
      U.setAlliance(effect.to, "vertical")
    elseif effect.to.chained or U.getAlliance(effect.to) == "vertical" then
      U.setAlliance(effect.to, "horizontal")
    elseif U.getAlliance(effect.to) == "horizontal" then
      effect.to:reset()
    end
  end,
})

skill:addEffect(fk.BeforeChainStateChange, {
  global = true,
  can_refresh = function (self, event, target, player, data)
    return target == player and player.chained and not data.prevented
  end,
  on_refresh = function (self, event, target, player, data)
    U.setAlliance(player, "")
  end,
})

skill:addEffect(fk.BeforeHpChanged, {
  global = true,
  can_refresh = function(self, event, target, player, data)
    return data.damageEvent and player == data.damageEvent.from and U.getAlliance(player) == U.getAlliance(target)
  end,
  on_refresh = function(self, event, target, player, data)
    data.damageEvent.extra_data = data.damageEvent.extra_data or {}
    data.damageEvent.extra_data.hanqing_alliance_check = true
  end,
})

skill:addEffect(fk.Damage, {
  global = true,
  can_refresh = function (self, event, target, player, data)
    return target == player and (data.extra_data or {}).hanqing_alliance_check
  end,
  on_refresh = function (self, event, target, player, data)
    U.setAlliance(player, "")
  end,
})

skill:addAI(nil, "__card_skill")

return skill
